﻿using System;
using System.Drawing;

namespace Lant.Net.Model
{
	public partial class PukeCard : System.Windows.Forms.PictureBox
	{
		private int _Index = -1;
		private PukeInfo _PukeInfo = null;
		private int _Number = -1;
		private int _Sign = -1;
		private bool _Selected = false;
		private bool _Display = true;
		private bool _Clicked = false;
		private EventHandler _ClickEventHandler = null;
		private EventHandler _DoubleClickEventHandler = null;

		private int _TopOffSet = 0;
		private int _ChangeSign = -1;
		private int _ChangeNumber = -1;

		public int TopOffSet { get { return _TopOffSet; } }
		//private bool _Enabled = false;

		/// <summary>
		/// 获取或设置是否显示牌面
		/// </summary>
		public bool Display { get { return _Display; } set { if (_Display != value) { _Display = value; this.SetDisPlay(); } } }
		/// <summary>
		/// 获取或设置是否可以点击
		/// </summary>
		public bool Clicked { get { return _Clicked; } set { if (_Clicked != value) { _Clicked = value; this.Click -= _ClickEventHandler; if (_DoubleClickEventHandler != null)this.DoubleClick -= _DoubleClickEventHandler; if (this._Clicked) { this.Click += _ClickEventHandler; if (_DoubleClickEventHandler != null)this.DoubleClick += _DoubleClickEventHandler; } } } }
		/// <summary>
		/// 获取是否是选择项(出牌的时候)
		/// </summary>
		public bool Selected { get { return _Selected; } }
		/// <summary>
		/// 获取索引Id
		/// </summary>
		public int Index { get { return _Index; } }
		/// <summary>
		/// 获取下一个牌面值
		/// </summary>
		public int NextNumber { get { return this._Number + 1 == 14 ? 1 : this._Number + 1; } }
		/// <summary>
		/// 获取上一个牌面值
		/// </summary>
		public int PrevNumber { get { return this._Number - 1 == 0 ? 13 : this._Number - 1; } }
		/// <summary>
		/// 获取牌面真实值
		/// </summary>
		public int Number { get { return _Number; } }
		/// <summary>
		/// 获取牌面显示值
		/// </summary>
		public string NumberDisplay { get { return _PukeInfo.GetCardNumberDisplay(this._Number); } }
		/// <summary>
		/// 获取牌面真实标记(红黑梅方)
		/// </summary>
		public int Sign { get { return _Sign; } }
		/// <summary>
		/// 获取牌面显示标记
		/// </summary>
		public string SignDisplay { get { return _PukeInfo.GetCardSignDisplay(this._Sign); } }
		/// <summary>
		/// 获取或设置变牌后标记(红黑梅方)
		/// </summary>
		public int ChangeSign { set { _ChangeSign = value; } get { return _ChangeSign; } }
		/// <summary>
		/// 获取或设置变牌后牌面值
		/// </summary>
		public int ChangeNumber { get { return _ChangeNumber; } set { _ChangeNumber = value; } }
		/// <summary>
		/// 获取或设置双击事件
		/// </summary>
		public EventHandler DoubleClickEventHandler { get { return _DoubleClickEventHandler; } set { _DoubleClickEventHandler = value; } }

		public PukeCard(PukeInfo thisCard, int thisValue)
		{
			this._PukeInfo = thisCard;
			this._Index = thisValue;
			this._Number = _PukeInfo.GetCardNumber(thisValue);
			this._Sign = _PukeInfo.GetCardSign(thisValue);
			this._ClickEventHandler = new EventHandler(PukeCard_Click);
			//this._DoubleClickEventHandler = new EventHandler(PukeCard_DoubleClick);
			this.SetDisPlay();
		}
		private void SetDisPlay()
		{
			this.Size = this._PukeInfo.CardSize;
			Point location = _PukeInfo.GetCardLocation(_Index);

			Image tempImg = new Bitmap(this._PukeInfo.CardSize.Width, this._PukeInfo.CardSize.Height);
			Graphics g = Graphics.FromImage(tempImg);
			if (!_Display)
			{
				location = _PukeInfo.GetCardLocation(54);
			}
			g.DrawImage(this._PukeInfo.Card, new Rectangle(0, 0, this._PukeInfo.Card.Width, this._PukeInfo.Card.Height), new Rectangle(location.X, location.Y, this._PukeInfo.Card.Width, this._PukeInfo.Card.Height), GraphicsUnit.Pixel);
			//g.DrawString(this._Number.ToString(), new Font("宋体", 10, FontStyle.Regular), new SolidBrush(Color.Red), 0, 25);
			this.Image = tempImg;
		}

		void PukeCard_Click(object sender, EventArgs e)
		{
			//System.Windows.Forms.MessageBox.Show(this._Index + "," + this._Number + "," + this.Sign);
			if (_Selected)
			{
				this.Top += 10;
				this._TopOffSet = 0;
			}
			else
			{
				this.Top -= 10;
				this._TopOffSet = 10;
			}
			_Selected = !_Selected;
		}
		public override string ToString()
		{
			return "puke";
		}
	}
}
